<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>pytest提高1--fixture | 移动端测试课程介绍</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-chapter-fold/chapter-fold.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day3/pytest_prov2.html" />
    
    
    <link rel="prev" href="../day3/middle.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.5"
        data-chapter-title="pytest提高1--fixture"
        data-filepath="day3/pytest_prov1.md"
        data-basepath=".."
        data-revision="Sat Jun 08 2019 07:44:25 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day1/readme.html">
            
                
                    <a href="../day1/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        移动入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day1/app_test_knowledge.html">
            
                
                    <a href="../day1/app_test_knowledge.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        移动端测试概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day1/create_env/readme.html">
            
                
                    <a href="../day1/create_env/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        移动测试环境搭建搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="day1/create_env/java.html">
            
                
                    <a href="../day1/create_env/java.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.1.</b>
                        
                        Java环境搭建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="day1/create_env/sdk.html">
            
                
                    <a href="../day1/create_env/sdk.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.2.</b>
                        
                        Android sdk 环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="day1/create_env/geny.html">
            
                
                    <a href="../day1/create_env/geny.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.3.</b>
                        
                        Android 虚拟机安装
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day1/command_adb.html">
            
                
                    <a href="../day1/command_adb.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        ADB命令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day1/app_tools.html">
            
                
                    <a href="../day1/app_tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        移动端自动化工具
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="day1/appium_env.html">
            
                
                    <a href="../day1/appium_env.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        appium环境搭建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="day1/start_appium.html">
            
                
                    <a href="../day1/start_appium.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        appium入门案例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day2/readme.html">
            
                
                    <a href="../day2/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        app操作api
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day2/base_api.html">
            
                
                    <a href="../day2/base_api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        app基础操作api
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="day2/uiauto.html">
            
                
                    <a href="../day2/uiauto.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        uiautomatorviewer工具
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day2/location_element.html">
            
                
                    <a href="../day2/location_element.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        app元素定位操作api
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day2/get_infor.html">
            
                
                    <a href="../day2/get_infor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        根据元素获取信息api
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day2/element_event.html">
            
                
                    <a href="../day2/element_event.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        元素事件操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="day2/touch_action.html">
            
                
                    <a href="../day2/touch_action.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        模拟手势操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="day2/phone_api.html">
            
                
                    <a href="../day2/phone_api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        手机操作api
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day3/readme.html">
            
                
                    <a href="../day3/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Pytest
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day3/install_introduction.html">
            
                
                    <a href="../day3/install_introduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        pytest安装和介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day3/setup_teardown.html">
            
                
                    <a href="../day3/setup_teardown.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        setup和teardown函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day3/pytest_config.html">
            
                
                    <a href="../day3/pytest_config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        pytest的配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="day3/middle.html">
            
                
                    <a href="../day3/middle.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        pytest常用插件
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.5" data-path="day3/pytest_prov1.html">
            
                
                    <a href="../day3/pytest_prov1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        pytest提高1--fixture
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="day3/pytest_prov2.html">
            
                
                    <a href="../day3/pytest_prov2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        pytest提高2
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day4/readme.html">
            
                
                    <a href="../day4/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        PO模式
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day4/introduction.html">
            
                
                    <a href="../day4/introduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        PO模式介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day4/project_plan.html">
            
                
                    <a href="../day4/project_plan.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        项目准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day4/logic.html">
            
                
                    <a href="../day4/logic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        按照业务抽取代码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day4/find_element.html">
            
                
                    <a href="../day4/find_element.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        抽取的find_element
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day4/base_action.html">
            
                
                    <a href="../day4/base_action.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        抽取基础动作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day5/readme.html">
            
                
                    <a href="../day5/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        数据驱动--Yaml
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day5/introduction_yaml.html">
            
                
                    <a href="../day5/introduction_yaml.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        Yaml介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day5/pyyaml.html">
            
                
                    <a href="../day5/pyyaml.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        python解析yaml文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day5/yaml_data.html">
            
                
                    <a href="../day5/yaml_data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        yaml数据驱动案例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day6/readme.html">
            
                
                    <a href="../day6/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Allure报告和Jenkins集成
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day6/allure.html">
            
                
                    <a href="../day6/allure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        allure介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="day6/pytest_allure.html">
            
                
                    <a href="../day6/pytest_allure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        pytest中使用allure
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day6/jenkins.html">
            
                
                    <a href="../day6/jenkins.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        Jenkins安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="day6/jenkins_git_allure_email.html">
            
                
                    <a href="../day6/jenkins_git_allure_email.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        Jenkins持续集成配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >移动端测试课程介绍</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="pytest&#x63D0;&#x9AD8;fixture">pytest&#x63D0;&#x9AD8;--fixture</h1>
<h2 id="&#x5B66;&#x4E60;&#x76EE;&#x6807;">&#x5B66;&#x4E60;&#x76EE;&#x6807;</h2>
<ul>
<li>&#x638C;&#x63E1;fixture&#x7684;&#x4F5C;&#x7528;</li>
<li>&#x638C;&#x63E1;fixture&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;</li>
</ul>
<hr>
<h3 id="1-&#x7B80;&#x4ECB;">1. &#x7B80;&#x4ECB;</h3>
<p>fixture&#x662F;pytest&#x7279;&#x6709;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5B83;&#x7528;pytest.fixture&#x6807;&#x8BC6;&#xFF0C;&#x4EE5;<strong>&#x88C5;&#x9970;&#x5668;&#x5F62;&#x5F0F;&#x5B9A;&#x4E49;&#x5728;&#x51FD;&#x6570;&#x4E0A;&#x9762;</strong>,&#x5728;&#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x6B64;&#x51FD;&#x6570;&#x540D;&#x79F0;&#x505A;&#x4E3A;&#x4F20;&#x5165;&#x53C2;&#x6570;&#xFF0C;pytest&#x5C06;&#x4F1A;&#x4EE5;&#x4F9D;&#x8D56;&#x6CE8;&#x5165;&#x65B9;&#x5F0F;&#xFF0C;&#x5C06;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F5C;&#x4E3A;&#x6D4B;&#x8BD5;&#x51FD;&#x6570;&#x7684;&#x4F20;&#x5165;&#x53C2;&#x6570;.</p>
<p>fixture&#x6709;&#x660E;&#x786E;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x5728;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#xFF0C;&#x6A21;&#x5757;&#xFF0C;&#x7C7B;&#x6216;&#x6574;&#x4E2A;&#x5DE5;&#x7A0B;&#x8C03;&#x7528;&#x5B83;&#x65F6;&#x4F1A;&#x88AB;&#x6FC0;&#x6D3B;&#x3002;</p>
<p>fixture&#x662F;&#x57FA;&#x4E8E;&#x6A21;&#x5757;&#x6765;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x6BCF;&#x4E2A;fixture&#x7684;&#x540D;&#x5B57;&#x5C31;&#x53EF;&#x4EE5;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;fixture&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x81EA;&#x8EAB;&#x4E5F;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x5176;&#x4ED6;&#x7684;fixture.</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A;fixture&#x770B;&#x505A;&#x662F;&#x8D44;&#x6E90;&#xFF0C;&#x5728;&#x4F60;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x6267;&#x884C;&#x4E4B;&#x524D;&#x9700;&#x8981;&#x53BB;&#x914D;&#x7F6E;&#x8FD9;&#x4E9B;&#x8D44;&#x6E90;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x540E;&#x9700;&#x8981;&#x53BB;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x3002;&#x6BD4;&#x5982;module&#x7C7B;&#x578B;&#x7684;fixture&#xFF0C;&#x9002;&#x5408;&#x4E8E;&#x90A3;&#x4E9B;&#x8BB8;&#x591A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x90FD;&#x53EA;&#x9700;&#x8981;&#x6267;&#x884C;&#x4E00;&#x6B21;&#x7684;&#x64CD;&#x4F5C;&#x3002;</p>
<p>fixture&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x53C2;&#x6570;&#x5316;&#x529F;&#x80FD;&#xFF0C;&#x6839;&#x636E;&#x914D;&#x7F6E;&#x548C;&#x4E0D;&#x540C;&#x7EC4;&#x4EF6;&#x6765;&#x9009;&#x62E9;&#x4E0D;&#x540C;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<h3 id="2-fixture&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;">2. fixture&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;</h3>
<p>fixture&#x4FEE;&#x9970;&#x5668;&#x6765;&#x6807;&#x8BB0;&#x56FA;&#x5B9A;&#x7684;&#x5DE5;&#x5382;&#x51FD;&#x6570;,&#x5728;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#xFF0C;&#x6A21;&#x5757;&#xFF0C;&#x7C7B;&#x6216;&#x6574;&#x4E2A;&#x5DE5;&#x7A0B;&#x8C03;&#x7528;&#x5B83;&#x65F6;&#x4F1A;&#x88AB;&#x6FC0;&#x6D3B;&#x5E76;&#x4F18;&#x5148;&#x6267;&#x884C;,<strong>&#x901A;&#x5E38;&#x4F1A;&#x88AB;&#x7528;&#x4E8E;&#x5B8C;&#x6210;&#x9884;&#x7F6E;&#x5904;&#x7406;&#x548C;&#x91CD;&#x590D;&#x64CD;&#x4F5C;</strong>&#x3002;</p>
<p>&#x4F8B;&#x5982;,</p>
<ul>
<li>&#x5728;&#x6D4B;&#x8BD5;&#x7F51;&#x7AD9;&#x7684;&#x529F;&#x80FD;&#x65F6;&#xFF0C;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x90FD;&#x8981;&#x767B;&#x5F55;&#x548C;&#x9000;&#x51FA;&#xFF0C;&#x5229;&#x7528;fixture&#x5C31;&#x53EF;&#x4EE5;&#x53EA;&#x505A;&#x4E00;&#x6B21;&#xFF0C;&#x5426;&#x5219;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x90FD;&#x8981;&#x505A;&#x8FD9;&#x4E24;&#x6B65;&#x4E5F;&#x662F;&#x5197;&#x4F59;&#x3002;</li>
<li>&#x5B8C;&#x6210;setup&#x548C;teardown&#x64CD;&#x4F5C;,&#x5904;&#x7406;&#x6570;&#x636E;&#x5E93;&#x6216;&#x6587;&#x4EF6;&#x7684;&#x6253;&#x5F00;&#x3001;&#x5173;&#x95ED;&#x64CD;&#x4F5C;</li>
<li>&#x51C6;&#x5907;&#x6D4B;&#x8BD5;&#x6570;&#x636E;.&#x5C06;&#x6570;&#x636E;&#x63D0;&#x524D;&#x5199;&#x5165;&#x6570;&#x636E;&#x5E93;&#x6216;&#x901A;&#x8FC7;params&#x8FD4;&#x56DE;&#x7ED9;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;</li>
</ul>
<h3 id="3-&#x4F7F;&#x7528;&#x65B9;&#x6CD5;">3. &#x4F7F;&#x7528;&#x65B9;&#x6CD5;</h3>
<pre><code>pytest.fixture(scope=&apos;function&apos;, params=None, autouse=False, ids=None, name=None)

&#x5E38;&#x7528;&#x53C2;&#x6570;&#x89E3;&#x91CA;:
-    scope: &#x88AB;&#x6807;&#x8BB0;&#x65B9;&#x6CD5;&#x7684;&#x4F5C;&#x7528;&#x57DF;;
        &quot;function&quot;: &#x9ED8;&#x8BA4;&#x503C;,&#x8868;&#x793A;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x90FD;&#x8981;&#x6267;&#x884C;&#x4E00;&#x6B21;
        &quot;class&quot;: &#x4F5C;&#x7528;&#x4E8E;&#x6574;&#x4E2A;&#x7C7B;, &#x8868;&#x793A;&#x6BCF;&#x4E2A;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x6B21;
        &quot;module&quot;: &#x4F5C;&#x7528;&#x4E8E;&#x6574;&#x4E2A;&#x6A21;&#x5757;, &#x6BCF;&#x4E2A;module&#x7684;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x6B21;.
        &quot;session&quot;: &#x4F5C;&#x7528;&#x4E8E;&#x6574;&#x4E2A;session, &#x6BCF;&#x6B21;session&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x6B21;. &#x26A0;&#xFE0F;(&#x6B64;&#x65B9;&#x6CD5;&#x614E;&#x7528;!!)

- params: list&#x7C7B;&#x578B;,&#x9ED8;&#x8BA4;None, &#x63A5;&#x6536;&#x53C2;&#x6570;&#x503C;,&#x5BF9;&#x4E8E;param&#x91CC;&#x9762;&#x7684;&#x6BCF;&#x4E2A;&#x503C;&#xFF0C;fixture&#x90FD;&#x4F1A;&#x53BB;&#x904D;&#x5386;&#x6267;&#x884C;&#x4E00;&#x6B21;.
- autouse: &#x662F;&#x5426;&#x81EA;&#x52A8;&#x8FD0;&#x884C;,&#x9ED8;&#x8BA4;&#x4E3A;false, &#x4E3A;true&#x65F6;&#x6B64;session&#x4E2D;&#x7684;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x51FD;&#x6570;&#x90FD;&#x4F1A;&#x8C03;&#x7528;fixture

&#x26A0;&#xFE0F; &#x4EE5;&#x4E0A;&#x7684;&#x6240;&#x6709;&#x53C2;&#x6570;&#x4E5F;&#x53EF;&#x4EE5;&#x4E0D;&#x4F20;
</code></pre><h3 id="4-&#x4F7F;&#x7528;&#x6848;&#x4F8B;">4. &#x4F7F;&#x7528;&#x6848;&#x4F8B;</h3>
<h4 id="41-&#x57FA;&#x672C;&#x4F7F;&#x7528;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x5E94;&#x7528;">4.1 &#x57FA;&#x672C;&#x4F7F;&#x7528;--&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x5E94;&#x7528;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pytest

<span class="hljs-decorator">@pytest.fixture()</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">before</span><span class="hljs-params">()</span>:</span>
  print(<span class="hljs-string">&quot;-----&gt; &#x5728;&#x6BCF;&#x4E2A;&#x51FD;&#x6570;&#x524D;&#x6267;&#x884C;&quot;</span>)


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_1</span><span class="hljs-params">(before)</span>:</span>
  print(<span class="hljs-string">&apos;-----&gt; test_1&apos;</span>)
  <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_2</span><span class="hljs-params">(before)</span>:</span>
  print(<span class="hljs-string">&quot;----&gt; test_2&quot;</span>)
  <span class="hljs-keyword">assert</span> <span class="hljs-number">0</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
  pytest.main(<span class="hljs-string">&quot;-s test_1.py&quot;</span>)
</code></pre>
<h4 id="42-&#x57FA;&#x672C;&#x4F7F;&#x7528;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x5E94;&#x7528;">4.2 &#x57FA;&#x672C;&#x4F7F;&#x7528;--&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x5E94;&#x7528;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pytest

<span class="hljs-decorator">@pytest.fixture()</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">before</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&apos;\nbefore each test&apos;</span>)

<span class="hljs-decorator">@pytest.mark.usefixtures(&quot;before&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_1</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&apos;---&gt;test_1&apos;</span>)

<span class="hljs-decorator">@pytest.mark.usefixtures(&quot;before&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_2</span><span class="hljs-params">(before)</span>:</span>
  print(<span class="hljs-string">&quot;----&gt; test_2&quot;</span>)


<span class="hljs-decorator">@pytest.mark.usefixtures(&quot;before&quot;)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test2</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_5</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;test_5&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_6</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;test_6&apos;</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    pytest.main(<span class="hljs-string">&quot;-s test_1.py&quot;</span>)
</code></pre>
<p>&#x57FA;&#x672C;&#x4F7F;&#x7528;&#x4E2D;&#x6211;&#x4EEC;&#x53D1;&#x73B0;&#x7ED3;&#x679C;&#x57FA;&#x672C;&#x662F;&#x7C7B;&#x4F3C;&#x7684;,&#x90FD;&#x662F;&#x5728;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x51FD;&#x6570;&#x524D;&#x88AB;&#x8C03;&#x7528;&#x6267;&#x884C;.</p>
<h4 id="43-&#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014;&#x8BBE;&#x7F6E;&#x4E3A;&#x9ED8;&#x8BA4;&#x542F;&#x52A8;&#x5F62;&#x5F0F;">4.3 &#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014;&#x8BBE;&#x7F6E;&#x4E3A;&#x9ED8;&#x8BA4;&#x542F;&#x52A8;&#x5F62;&#x5F0F;</h4>
<p><strong>&#x8BBE;&#x7F6E;true,&#x662F;&#x81EA;&#x52A8;&#x8FD0;&#x884C;, &#x4EE3;&#x8868;&#x8FD9;&#x4E2A;session&#x7684;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x51FD;&#x6570;&#x90FD;&#x4F1A;&#x8C03;&#x7528;</strong></p>
<pre><code>import pytest

@pytest.fixture(autouse=True)
def before():
    print(&apos;\nbefore each test&apos;)


class Test2:
    def test_5(self):
        print(&apos;test_5&apos;)

    def test_6(self):
        print(&apos;test_6&apos;)

if __name__ == &quot;__main__&quot;:
    pytest.main(&quot;-s test_1.py&quot;)
</code></pre><h4 id="44-&#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014;&#x8BBE;&#x7F6E;&#x4F5C;&#x7528;&#x57DF;&#x4E3A;function">4.4 &#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014;&#x8BBE;&#x7F6E;&#x4F5C;&#x7528;&#x57DF;&#x4E3A;function</h4>
<p><strong>&#x8BBE;&#x7F6E;&#x4E3A;function,  &#x8868;&#x793A;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x90FD;&#x8981;&#x6267;&#x884C;&#x4E00;&#x6B21;</strong></p>
<pre><code>import pytest

@pytest.fixture(scope=&quot;function&quot;, autouse=True)
def before():
    print(&apos;\nbefore each test&apos;)

class Test2:
    def test_5(self):
        print(&apos;test_5&apos;)

    def test_6(self):
        print(&apos;test_6&apos;)

if __name__ == &quot;__main__&quot;:
    pytest.main(&quot;-s test_1.py&quot;)
</code></pre><h4 id="45-&#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014;-&#x8BBE;&#x7F6E;&#x4F5C;&#x7528;&#x57DF;&#x4E3A;class">4.5 &#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014; &#x8BBE;&#x7F6E;&#x4F5C;&#x7528;&#x57DF;&#x4E3A;class</h4>
<p><strong>&#x4F5C;&#x7528;&#x4E8E;&#x6574;&#x4E2A;&#x7C7B;, &#x8868;&#x793A;&#x6BCF;&#x4E2A;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x6B21;</strong></p>
<pre><code>import pytest


@pytest.fixture(scope=&quot;class&quot;, autouse=True)
def before():
    print(&apos;\nbefore each test&apos;)


@pytest.mark.usefixtures(&quot;before&quot;)
def test_1():
    print(&apos;---&gt;test_1&apos;)

class Test2:
    def test_5(self):
        print(&apos;test_5&apos;)

    def test_6(self):
        print(&apos;test_6&apos;)

if __name__ == &quot;__main__&quot;:
    pytest.main(&quot;-s test_1.py&quot;)
</code></pre><h4 id="46-&#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014;-&#x8BBE;&#x7F6E;&#x4F5C;&#x7528;&#x57DF;&#x4E3A;module">4.6 &#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014; &#x8BBE;&#x7F6E;&#x4F5C;&#x7528;&#x57DF;&#x4E3A;module</h4>
<p><strong>&#x4F5C;&#x7528;&#x4E8E;&#x6574;&#x4E2A;&#x6A21;&#x5757;, &#x8868;&#x793A;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#x7684;&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x6B21;fixture</strong>, &#x4F8B;&#x5982;&#x6240;&#x6709;&#x7684;test&#x90FD;&#x9700;&#x8981;&#x8FDE;&#x63A5;&#x540C;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;,&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6B64;&#x65B9;&#x6CD5;,&#x5BF9;&#x4E8E;&#x6B64;&#x6A21;&#x5757;&#x7684;&#x6240;&#x6709;test&#x90FD;&#x6709;&#x6548;</p>
<pre><code>import pytest


@pytest.fixture(scope=&quot;module&quot;, autouse=True)
def before():
    print(&apos;\nbefore each test&apos;)


@pytest.mark.usefixtures(&quot;before&quot;)
def test_1():
    print(&apos;---&gt;test_1&apos;)

class Test2:
    def test_5(self):
        print(&apos;test_5&apos;)

    def test_6(self):
        print(&apos;test_6&apos;)

if __name__ == &quot;__main__&quot;:
    pytest.main(&quot;-s test_1.py&quot;)
</code></pre><h4 id="47-&#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014;-fixture-&#x8FD4;&#x56DE;&#x503C;">4.7 &#x5E26;&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x2014; fixture &#x8FD4;&#x56DE;&#x503C;</h4>
<p>&#x9ED8;&#x8BA4;&#x4E3A;none, &#x4F20;&#x5165;list&#x7C7B;&#x578B;&#x53C2;&#x6570;, fixture&#x4F1A;&#x904D;&#x5386;&#x8C03;&#x7528;list&#x4E2D;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;.</p>
<pre><code>import pytest

@pytest.fixture(params=[1, 2, 3])
def need_data(request): # &#x4F20;&#x5165;&#x53C2;&#x6570;request &#x7CFB;&#x7EDF;&#x5C01;&#x88C5;&#x53C2;&#x6570;
      return request.param # &#x53D6;&#x5217;&#x8868;&#x4E2D;&#x5355;&#x4E2A;&#x503C;&#xFF0C;&#x9ED8;&#x8BA4;&#x7684;&#x53D6;&#x503C;&#x65B9;&#x5F0F;

class Test_ABC:
    def test_a(self,need_data):
        print(&quot;test_a &#x7684;&#x503C;&#x662F; %s&quot; % need_data)
        assert need_data != 3 # &#x65AD;&#x8A00;need_data&#x4E0D;&#x7B49;&#x4E8E;3

if __name__ == &apos;__main__&apos;:
    pytest.main(&quot;-s  test_abc.py&quot;)
</code></pre><h3 id="5-&#x603B;&#x7ED3;">5. &#x603B;&#x7ED3;</h3>
<p>&#x4F7F;&#x7528;fixture&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E9B;&#x91CD;&#x590D;&#x7684;&#x64CD;&#x4F5C;&#x8FDB;&#x884C;&#x7B80;&#x5316;&#x6216;&#x8005;&#x63D0;&#x524D;&#x5904;&#x7406;,&#x6765;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x7684;&#x6548;&#x7387;.&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x6709;&#x5F88;&#x591A;&#x8981;&#x7075;&#x6D3B;&#x4F7F;&#x7528;.</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day3/middle.html" class="navigation navigation-prev " aria-label="Previous page: pytest常用插件"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day3/pytest_prov2.html" class="navigation navigation-next " aria-label="Next page: pytest提高2"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"expandable-chapters-small":{},"splitter":{},"chapter-fold":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
